package com.zz.bid.web.controller;

import com.zz.bid.exception.base.BidException;
import com.zz.bid.pojo.entity.Menu;
import com.zz.bid.pojo.to.LoginTO;
import com.zz.bid.pojo.to.ResponseWrapper;
import com.zz.bid.pojo.to.UserTO;
import com.zz.bid.service.MenuService;
import com.zz.bid.service.UserService;
import com.zz.bid.web.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class LoginController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    @PostMapping("/login")
    public ResponseWrapper login(@RequestBody LoginTO loginTO) {
        try {
            UserTO userTO = userService.authenticate(loginTO.getUserName(), loginTO.getPassword());
            return ResponseWrapper.builder().data(userTO).status(HttpStatus.OK.value()).path(request.getServletPath())
                    .build();
        } catch (BidException e) {
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            return ResponseWrapper.builder().message(e.getMessage()).status(HttpStatus.UNAUTHORIZED.value())
                    .path(request.getServletPath()).build();
        }
    }

    @PostMapping("/logout")
    public ResponseWrapper logout(@RequestHeader String token) {
        try {
            userService.logout(token);
            return ResponseWrapper.builder().status(HttpStatus.OK.value()).path(request.getServletPath()).build();
        } catch (BidException e) {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            return ResponseWrapper.builder().message(e.getMessage()).status(HttpStatus.INTERNAL_SERVER_ERROR.value())
                    .path(request.getServletPath()).build();
        }
    }

    @GetMapping("/getUserInfo")
    public ResponseWrapper getUserInfo(@RequestHeader("token") String token) {
        try {
            UserTO userTO = userService.getUserInfo(token);
            return ResponseWrapper.builder().data(userTO).path(request.getServletPath()).data(userTO)
                    .status(HttpStatus.OK.value()).build();
        } catch (BidException e) {
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            return ResponseWrapper.builder().message(e.getMessage()).status(HttpStatus.UNAUTHORIZED.value())
                    .path(request.getServletPath()).build();
        }
    }

    @GetMapping("/getMenuAccess")
    public ResponseWrapper getMenuAccess() {
        try {
            List<Menu> menus = menuService.getMenus();
            return ResponseWrapper.builder().status(HttpStatus.OK.value()).path(request.getServletPath()).data(menus)
                    .path(request.getServletPath()).build();
        } catch (BidException e) {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            return ResponseWrapper.builder().path(request.getServletPath())
                    .status(HttpStatus.INTERNAL_SERVER_ERROR.value()).message(e.getMessage()).build();
        }
    }

}
